package com.qx.chat.websocket.interceptor;

import java.util.Map;

import javax.servlet.http.HttpServletRequest;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.http.server.ServerHttpRequest;
import org.springframework.http.server.ServerHttpResponse;
import org.springframework.http.server.ServletServerHttpRequest;
import org.springframework.web.socket.WebSocketHandler;
import org.springframework.web.socket.server.support.HttpSessionHandshakeInterceptor;

/**
 * 功能说明：websocket连接的拦截器 有两种方式
 * 一种是实现接口HandshakeInterceptor，实现beforeHandshake和afterHandshake函数
 * 一种是继承HttpSessionHandshakeInterceptor，重载beforeHandshake和afterHandshake函数
 * 我这里是参照spring官方文档中的继承HttpSessionHandshakeInterceptor的方式 作者：cyf(2016-01-25
 * 03:46)
 */
public class QxWebSocketHandshakeInterceptor extends HttpSessionHandshakeInterceptor {

	private static final Logger LOGGER = LoggerFactory.getLogger(QxWebSocketHandshakeInterceptor.class);

	/**
	 * 从请求中获取唯一标记参数，填充到数据传递容器attributes
	 * 
	 * @param serverHttpRequest
	 * @param serverHttpResponse
	 * @param wsHandler
	 * @param attributes
	 * @return
	 * @throws Exception
	 */
	@Override
	public boolean beforeHandshake(ServerHttpRequest serverHttpRequest, ServerHttpResponse serverHttpResponse,
			WebSocketHandler wsHandler, Map<String, Object> attributes) throws Exception {
		ServletServerHttpRequest servletRequest = (ServletServerHttpRequest) serverHttpRequest;
		HttpServletRequest request = servletRequest.getServletRequest();
		attributes.put("loginName", request.getParameter("loginName"));
		LOGGER.info("loginname loaded session");
		super.beforeHandshake(serverHttpRequest, serverHttpResponse, wsHandler, attributes);
		return true;
	}

	@Override
	public void afterHandshake(ServerHttpRequest request, ServerHttpResponse response, WebSocketHandler wsHandler,
			Exception ex) {
		super.afterHandshake(request, response, wsHandler, ex);
	}

}